package com.wandian.weather.mvvm.constellation;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.R;
import com.wandian.weather.adapter.ConstellationAdapter;
import com.wandian.weather.bean.XingZuoBean;
import com.wandian.weather.databinding.WeatherActivityConstelllationBinding;
import com.wandian.weather.mvvm.rvpager.GalleryLayoutManager;
import com.wandian.weather.mvvm.rvpager.ScaleTransformer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 星座
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_WEATHER_CONSTELLATION)
public class ConstellationActivity extends BaseMVVMActivity<ConstellationViewModel> {
    private WeatherActivityConstelllationBinding dataBinding;
    private ConstellationAdapter mAdapter;
    private List<XingZuoBean> xingZuoBeanList = new ArrayList<>();
    private XingZuoBean xingZuoBean;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.weather_activity_constelllation);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initClink();
        initViewData();
        initTopRecycle();
        mViewModel.getConstellationData();
        mViewModel.getItemConstellationData("shuiping");
        mViewModel.getConstellationDetails();
    }

    private void initViewData(){

        mViewModel.xingZuoListBean.observe(this,xingZuoListBean->{
            if(!UIUtils.isEmpty(xingZuoListBean) && xingZuoListBean.size()>0){
                this.xingZuoBeanList = xingZuoListBean;
                mAdapter.setNewData(xingZuoListBean);
            }
        });
    }

    private void initTopRecycle(){
        GalleryLayoutManager layoutManager1 = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
        layoutManager1.attach(dataBinding.recyclerTop, 0);
        layoutManager1.setItemTransformer(new ScaleTransformer());
        layoutManager1.setOnItemSelectedListener(new GalleryLayoutManager.OnItemSelectedListener() {
            @Override
            public void onItemSelected(RecyclerView recyclerView, View item, int position) {
                xingZuoBean = xingZuoBeanList.get(position);
                if(!UIUtils.isEmpty(xingZuoBean)){
                    mViewModel.getItemConstellationData(xingZuoBean.getStar());
                }
            }
        });
        mAdapter = new ConstellationAdapter(R.layout.weather_item_constellation_layout);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                dataBinding.recyclerTop.smoothScrollToPosition(position);
            }
        });
        dataBinding.recyclerTop.setAdapter(mAdapter);
    }


    private void initClink(){
        dataBinding.ivBack.setOnClickListener(v -> {
            finish();
        });

    }



}
